<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Post a micro news!
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.2
 * 
 * 0.2)  Inherited from Addaction.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_addaction.php';
require_once INSTALLDIR . '/lib/slh_rights.php';

require_once INSTALLDIR . '/widgets/slh_form_addmicronews.php';

require_once INSTALLDIR . '/classes/MicroNews.php';
require_once INSTALLDIR . '/classes/Picture.php';

class AddmicronewsAction extends AddAction
{
	function title()
	{
		return '添加微新闻';
	}
	
	function prepare($args)
	{
		if ( !Rights::hasRight(slh_get_userrights(), RIGHT_NEWS)) {
			return false;
		}
		
		return parent::prepare($args);
	}
	
	/**
	 * Add the ajaxfileupload stylesheet.
	 */
	function showStylesheets()
	{
		parent::showStylesheets();
		
		$this->cssLink('theme/base/charCount/charCount.css', 'base', 'screen, projection, tv');
		$this->cssLink('theme/base/css/jquery.ajaxfileupload.css', 'base', 'screen, projection, tv');
	}
	
	/**
	 * Add the ajaxfileuploader scripts.
	 */
	function showScripts()
	{
		parent::showScripts();
	
		$this->script('charCount/charCount.js');
		$this->script('ajaxfileupload/jquery.ajaxfileupload.min.js');
		$this->script('action/addmicronews.go.js');
	}
	
	/**
	 * Shows the guidance.
	 * Overriding.
	 */
	function showGuidance()
	{
		// Html code here.
		$this->element('h3', null, '添加一条微新闻');
		
		$this->instruction('呃,说明下添加的具体是什么东东,怎么添加好了.');
		
		// If added successfully, then?
		if ($this->added) {
			$this->element('p', 'cls_p_success', '添加成功!');
			// Or show a nav.
		}
	}
	
	/**
	 * Shows the widgets like form, or etc.
	 * Overriding.
	 */
	function showForm()
	{
		$form = new AddmicronewsForm($this, $this->args);
		$form->show();
	}	
	
	/**
	 * Try to add the profile into the database.
	 */
	function tryAdd()
	{
		// CSRF protection and Refreshing-post protection.
		$this->checkSessionToken();
		
		$message	= $this->getTrimmedArg('message');
		$picture_id	= $this->getTrimmedArg('picture_id');
		$user_id = slh_get_userid();
		
				
// TODO: Validates the data.
		if ( !MicroNews::validateMessage($message)) {
			Picture::removeIfAiredPicture($picture_id);
			
			$this->showAction('微新闻的内容不合法, 是不是没有内容或者超过字数了?');
		} else {
			$micronews_id = MicroNews::add(array('message' => $message,
												 'picture_id' => $picture_id,
												 'user_id' => $user_id));
			
			if ( !$micronews_id) {
				$this->showAction('发布微新闻失败!');
				return;
			}
			
			// Success.
			// Unset the seesion token preventing post-again.
			$this->unsetSessionToken();
			
			// Unset the seted values.
			unset($this->args['message']);
			unset($this->args['picture_id']);
			
			$this->added = true;
			$this->showAction();
		}
	}
}

?>